app.controller("goodsController",function(
    $scope,
    $controller,
    $location,
    goodsService,
    uploadService,
    itemCatService,
    typeTemplateService){
    $controller('baseController',{$scope:$scope});

    $scope.entity = {goods:{},goodsDesc:{itemImages:[],customAttributeItems:[],specificationItems:[]},itemList:[]};
    $scope.goods_image = {};
    $scope.itemCat1List = [];
    $scope.itemCat2List = [];
    $scope.itemCat3List = [];
    $scope.typeTemplate = {};
    $scope.specList = [];


    $scope.selectItemCat1List = function(){
        itemCatService.findByParentId(0).then(function(result){
            $scope.itemCat1List = result.data;
        });
    }
    $scope.$watch("entity.goods.category1Id",function(newVal,oldVal){
        itemCatService.findByParentId(newVal).then(function(result){
            $scope.itemCat2List = result.data;
        });
    })
    $scope.$watch("entity.goods.category2Id",function(newVal,oldVal){
        itemCatService.findByParentId(newVal).then(function(result){
            $scope.itemCat3List = result.data;
        });
    })
    $scope.$watch("entity.goods.category3Id",function(newVal,oldVal){
        //根据模板ID  查询出模板对象
        itemCatService.findOne(newVal).then(function(result){
            $scope.entity.goods.typeTemplateId = result.data.typeId;
        });
    })
    $scope.$watch("entity.goods.typeTemplateId",function(newVal,oldVal){
        typeTemplateService.findOne(newVal).then(function(result){
            $scope.typeTemplate = result.data;
            $scope.typeTemplate.brandIds = JSON.parse($scope.typeTemplate.brandIds);
            var id = $location.search()['id'];
            if(!id){
                $scope.entity.goodsDesc.customAttributeItems = JSON.parse($scope.typeTemplate.customAttributeItems);
            }
        });

        //  根据模板ID 查询出  对应的 规格  及  规格选项
        typeTemplateService.findSpecList(newVal).then(function(result){
            $scope.specList = result.data;
        });
    })

    $scope.uploadImage = function(){
        uploadService.uploadImage().then(function(result){
            if(result.data.error==0){
                $scope.goods_image.url = result.data.url;
            }else {
                alert(result.data.message);
            }
        });
    }
    $scope.add_image = function(){
        $scope.entity.goodsDesc.itemImages.push($scope.goods_image);
    }

    $scope.save = function(){
        $scope.entity.goodsDesc.introduction = editor.html();
        goodsService.save($scope.entity).then(function(result){
            if(result.data.success){
                alert('保存成功');
                $scope.entity = {goods:{},goodsDesc:{},itemList:[]};
            }else {
                alert(result.data.message);
            }
        });
    }
    $scope.del_image = function(index){
        $scope.entity.goodsDesc.itemImages.splice(index,1);
    }
    $scope.updateSpecAttribute = function($event,text,optionName){
        // 更新   entity.goodsDesc.specificationItems
        // 判断  之前 是否有添加过  该规格属性
        var object = $scope.searchObjectByKey($scope.entity.goodsDesc.specificationItems,'attributeName',text);
        if(object!=null){
            if($event.target.checked){
                // 选择状态
                object.attributeValue.push(optionName);
            }else{
                // 取消选择状态
                var optionIndex = object.attributeValue.indexOf(optionName);
                object.attributeValue.splice(optionIndex,1);
                if(object.attributeValue.length==0){
                    var objIndex = $scope.entity.goodsDesc.specificationItems.indexOf(object);
                    $scope.entity.goodsDesc.specificationItems.splice(objIndex,1);
                }
            }
        }else {
            $scope.entity.goodsDesc.specificationItems.push({"attributeName":text,"attributeValue":[optionName]});
        }
    }
    $scope.createItemList = function(){
        //创建 itemList  sku集合
        $scope.entity.itemList = [{spec:{},price:0,num:0,status:'0',isDefault:'0'}];
        var items = $scope.entity.goodsDesc.specificationItems;
        for(var i=0;i<items.length;i++){
            $scope.entity.itemList = addColumn($scope.entity.itemList,items[i].attributeName,items[i].attributeValue);
        }

    }
    //                   数组1  sku数组     数组2  属性数组
    addColumn = function(itemList,attributeName,attributeValue){
        var newItemList = [];
        for(var i=0;i<itemList.length;i++){
            var oldItem = itemList[i];
            for(var j=0;j<attributeValue.length;j++){
                var newRow=JSON.parse(JSON.stringify(oldItem));
                newRow['spec'][attributeName]=attributeValue[j];
                newItemList.push(newRow);
            }
        }
        return newItemList;
    }

    $scope.findOne = function(){
        //获取到路径上传过来的  id 用$location 可以获取#？后面的参数
        var id= $location.search()['id'];//获取参数值
        if(id){
            //修改操作
            goodsService.findOne(id).then(function(result){
                $scope.entity = result.data;
                editor.html($scope.entity.goodsDesc.introduction);
                $scope.entity.goodsDesc.itemImages=
                    JSON.parse($scope.entity.goodsDesc.itemImages);
                //显示扩展属性
                $scope.entity.goodsDesc.customAttributeItems=
                    JSON.parse($scope.entity.goodsDesc.customAttributeItems);
                $scope.entity.goodsDesc.specificationItems=
                    JSON.parse($scope.entity.goodsDesc.specificationItems);
                for(var i=0;i<$scope.entity.itemList.length;i++){
                    $scope.entity.itemList[i].spec = JSON.parse($scope.entity.itemList[i].spec);
                }
            });
        }else {
            return;
        }
    }
    $scope.checkAttributeSelected = function(text,optionName){
        // 返回 boolean 值
        var object = $scope.searchObjectByKey($scope.entity.goodsDesc.specificationItems,'attributeName',text);
        if(object!=null){
            var objIndex = object.attributeValue.indexOf(optionName);
            if(objIndex==-1){
                return false;
            }else{
                return true;
            }
        }else {
            return false;
        }
    }


});